{  Statement: http://acmp.ru/?main=task&id_task=292
   Verdict: Accepted
}
uses
    SysUtils, Math;

var
    n:longint;

function prime(n:longint):boolean;

var
    i,kk:longint;

begin
result := true;
kk := trunc(sqrt(n));
for i:=2 to kk do
    if n mod i = 0 then begin
        result := false;
        exit;
    end;
if n = 1 then result := false;
end;

function get(n:longint):longint;
begin
result := 0;
while n > 0 do begin
    inc(result, n mod 10);
    n := n div 10;
end;
end;

begin
reset(input,'input.txt');
rewrite(output,'output.txt');
read(n);
while true do begin
    if prime(n) then begin
        write(n);
        halt(0);
    end;
    if n < 10 then begin
        write(0);
        halt(0);
    end;
    n := get(n);
end;
end.